"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lane.chang on '26/10/2024'
    comment: AIS服务相关接口
"""
from config import Config
from project.model.basic import Operator, Device
from project.model.micro_service import MicroService


class Zeus:

    @staticmethod
    async def do_login(code, password):
        """
        :param code:
        :param password:
        :return:
        """
        data = await MicroService(domain=Config.ZEUS_SERVER).post(url_path='/m/operator/login', code=code, password=password)

        return data

    @staticmethod
    async def get_login_info(token) -> Operator:
        """ 得到用户的登录信息
        :param token:
        :return:
        """
        data = await MicroService(domain=Config.ZEUS_SERVER, headers={'authorization': f'Bearer {token}'}).get(url_path='/m/operator/login_info')

        return Operator(**data)

    @staticmethod
    async def get_device(dev_id) -> Device:
        """
        :return:
        """
        data = await MicroService(domain=Config.ZEUS_SERVER).get(url_path='/u/device/detail', code=dev_id)

        device = Device(**data)
        device.dev_id = device.code

        return device

    @staticmethod
    async def get_device_relation_list(dev_id):
        """ 设备关联列表
        :return:
        """
        data = await MicroService(domain=Config.ZEUS_SERVER).get(url_path='/u/device/relation/list', code=dev_id)

        return data

    @staticmethod
    async def get_organization_list(codes=None, name=None, count=None):
        """ 取得机构列表
        """
        params = dict()
        if codes is not None:
            params['codes'] = codes
        if name is not None:
            params['name'] = name
        if count is not None:
            params['count'] = count

        data = await MicroService(domain=Config.ZEUS_SERVER).post(url_path='/b/organization/list', **params)

        return data

    @staticmethod
    async def get_operator_list(codes=None, name=None, count=None):
        """ 取得用户列表
        """
        params = dict()
        if codes is not None:
            params['codes'] = codes
        if name is not None:
            params['name'] = name
        if count is not None:
            params['count'] = count

        data = await MicroService(domain=Config.ZEUS_SERVER).post(url_path='/b/operator/list', **params)

        return data

    @staticmethod
    async def do_device_connect(dev_id):
        """ 设备连接服务器
        """
        try:
            await MicroService(domain=Config.ZEUS_SERVER).get(url_path='/b/device/connect', dev_id=dev_id)
        except:
            pass

    @staticmethod
    async def get_device_comm_list(dev_id, name):
        """ 设备通讯列表
        :return:
        """
        data = await MicroService(domain=Config.ZEUS_SERVER).get(url_path='/b/device/comm/list', dev_id=dev_id, name=name)

        return data

    @staticmethod
    async def switch_device_agent(organization_code, origin_agent_code, agent_code):
        """ 切换设备智能体
        :param organization_code:
        :param origin_agent_code:
        :param agent_code:
        :return:
        """
        data = await MicroService(domain=Config.ZEUS_SERVER).post(url_path='/b/device/agent/switch',
                                                                  organization_code=organization_code,
                                                                  origin_agent_code=origin_agent_code,
                                                                  agent_code=agent_code)

        return data

    @staticmethod
    async def get_device_list(agent_code=None, is_internal=None, count=None, organization_id=None):
        """ 取得设备列表
        :return:
        """
        params = dict()
        if agent_code is not None:
            params['agent_code'] = agent_code
        if is_internal is not None:
            params['is_internal'] = is_internal
        if count is not None:
            params['count'] = count
        if organization_id is not None:
            params['organization_id'] = organization_id

        data = await MicroService(domain=Config.ZEUS_SERVER).get(url_path='/b/device/list', **params)

        return data

    @staticmethod
    async def get_organization_detail(code=None, third_party_id=None):
        """ 获取机构详情
        :param code: 机构编号
        :param third_party_id: 第三方系统标识
        :return:
        """
        params = dict()
        if code:
            params['code'] = code
        if third_party_id:
            params['third_party_id'] = third_party_id
        data = await MicroService(domain=Config.ZEUS_SERVER).get(url_path='/b/organization/detail', **params)

        return data
